/*
 * @Description: 算法使用的小工具集
 * @Version: 
 * @Author: Weixin Zhu
 * @Date: 2020-11-25 13:27:09
 * @LastEditors: Weixin Zhu
 * @LastEditTime: 2020-12-11 17:45:34
 */
package com.zwx.algorithms.pub.util;

import java.util.concurrent.ThreadLocalRandom;

import com.zwx.algorithms.constant.AlgorithmsConstant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.RandomUtil;

public class AlgorithmsUtils {
    static final Logger log = LoggerFactory.getLogger(AlgorithmsUtils.class);

    private static final ThreadLocalRandom random = RandomUtil.getRandom();

    /**
     * @description: 自动产生一个正数数组，最大值为length，元素个数为length个
     * @param {int}
     *                  length
     * @return {*}
     */
    public static Integer[] generateRandomArray(int length) {
        Integer[] array = new Integer[length];
        for (int i = 0; i < length; i++) {
            array[i] = random.nextInt(length);
            // random.nextInt(MAX_VALUE);
            // log.debug("array[{}]:{}", i, array[i]);
        }
        // log.info("array size:{}", array.length);
        return array;
    }

    /**
     * @description: 通用方法，判断是否需要打印算法步骤
     * @param {*}
     * @return {*}
     */
    public static boolean needShowStep(int size) {
        if (AlgorithmsConstant.SHOW_LOOP_TIMES_FALG && size <= AlgorithmsConstant.SHOW_FLAG_BUT_WITH_MAX_VALUE) {
            return true;
        }
        return false;
    }

    /**
     * @description: 通用方法，判断是否需要打印算法步骤
     * @param {*}
     * @return {*}
     */
    public static void printSortedArray(Integer[] array) {
        if (AlgorithmsConstant.SHOW_SORTED_RESULT_FALG && array.length <= AlgorithmsConstant.SHOW_FLAG_BUT_WITH_MAX_VALUE) {
            log.info("sorted array result:-----------------------");
            for (int i = 0; i < array.length; i++) {
                log.info("sorted array[{}]:{}", i, array[i]);
            }
        }
    }

    public static void main(String[] args) {
        Integer[] originArray = null;
        StopWatch sw = DateUtil.createStopWatch("AlgorithmsUtils Test");
        sw.start();
        originArray = AlgorithmsUtils.generateRandomArray(10000);
        sw.stop();
        for (int i = 0; i < originArray.length; i++) {
            log.debug("array[{}]:{}", i, originArray[i]);
        }

        log.info("size:{},cost time:{}", originArray.length, sw.getLastTaskTimeMillis());
        log.info(sw.prettyPrint());
    }
}